import type { FormInstance } from 'antd/es/form';
import { ArrayHas, getEvenCount, getHistory, getHistoryStr, getNumberStr, getOddAndEven, getRemoveNumber, needRemove, trim } from '@/utils/format';
import { PageContainer } from '@ant-design/pro-components';
import { useModel } from '@umijs/max';
import { Button, Form, Input } from 'antd';
import { useEffect, useRef, useState } from 'react';
import {
  sampleSize, //获得随机数
  uniq, //去重
  take, //从0开始获得指定数组
  takeRight,
  orderBy,
  countBy,
  slice//裁剪数组
} from "lodash"
import getChooseList from './getChooseList';
import { getFilterResult } from './getFilterResult';
import HistoryList from '@/components/Guide/HistoryList';
import IndexLocation from '@/components/Guide/IndexLocation';
import Red33List from '@/components/Guide/Red33List';

window.take = take;

const HomePage: React.FC = () => {
  
  const [loading, setLoading] = useState(false);

  const [data, setData] = useState({
    historyListTop20:[],//最近的20期
    result:[],//所有可能的结果
    nearByList: [],//去掉上一期上下浮动20万条数据
    continue4:[],//去掉4/5/6连号
    continue3:[],//3连号
    oneMore456:[],//1区大于4 5 6个数
    twoMore456:[],//2区大于4 5 6个数
    threeMore456:[],//3区大于4 5 6个数
    evenCount6: [],//偶数6
    evenCount5: [],//偶数5
    oddCount6: [],//奇数6
    oddCount5: [],//奇数5
    drop1112And2223: [],//去掉对称数（比如01&02和32&33）
    red2BigRed1Nine:[],//红球2-红球1大于9
    red654321jinge:[],//红6-红5 && 红4-红3 && 红2-红1 的结果同时=1 2 3 4 5 6 7 8 9 10 11时
    red3164dengyu10:[],//红3-红1 = 10 && 统计红6-红4 = 10
    tongwei:[],//去掉出现3同尾，或者4个1同尾的情况
    top20Red21Red32Red43: [],//去掉最近20期，红2-红1，红3-红2，红4-红3的相同间隔的记录
    red6red1:[],//去掉红6-红1小于等于13
    dropTopOddAndEevn: [],//去掉最近20期的奇偶
    filterResult:[],//过滤后的号码
  });//所有结果
  const [buyResult, setBuyResult] = useState([]);//随机出来的要买的结果
  const [lastNum, setLastNum] = useState<[]>([]);//随机出来的要买的结果
  const [chooseList , setChooseList] = useState([]);//最终筛选的需要购买的列表
  const [gaopinOddQishu, setGaopinOddQishu] = useState({});//每期奇偶组合出现的高频次数统计
  
  let formRef = useRef<FormInstance<any>>();

  let {
    allRedBallList,
    allRedBallStrList,
    historyList,
    historyResultStrList,
    historyOddEvenList,
    historyOddEvetTongj
  } = useModel("ballModel")

  window.allRedBallList = allRedBallList;
  window.historyList = historyList;
  window.historyResultStrList = historyResultStrList;

  let {
    cishuListArr,
    cishuListObj,
    cishuArr,
    sortListObj//奇偶组合距离上次出现的期数距离作为key，出现多少次作为值
  } = historyOddEvetTongj;

  useEffect(()=>{
    let filterResult = getFilterResult({
      allRedBallList: allRedBallList,
      allRedBallStrList: allRedBallStrList,
      historyList: historyList,
      historyResultStrList: historyResultStrList,
      historyOddEvenList: historyOddEvenList
    });
    setData(filterResult);


    let removeRule = [2, 3, 1, 1, 8, 1]//根据杀号规则，计算出来的6个球要+的数量
    
    let lastRedBall = historyList[historyResultStrList[0]].number;//取最新的一期开奖结果作为样本，最新的一起在数组的第一个
    let removeNumber:any[] = getRemoveNumber(lastRedBall, removeRule);
    removeNumber = getNumberStr(removeNumber);//把数字都转为2个数字的字符串
    formRef.current.setFieldValue("filterNum", removeNumber.join(" "));
  },[]);

  return (
    <PageContainer ghost>
      <div>
        <p>原始数量：{data.result.length}，过滤后数量：{data.filterResult.length}，总共过滤掉了：{data.result.length - data.filterResult.length}</p>
        <p>筛除记录：</p>
        <p>去掉上一期上下浮动20万条数据：{data.nearByList.length}</p>
        <p>去掉4/5/6连号：{data.continue4.length}</p>
        <p>去掉3连号：{data.continue3.length}</p>
        <p>去掉1区大于4 5 6个数：{data.oneMore456.length}</p>
        <p>去掉2区大于4 5 6个数：{data.twoMore456.length}</p>
        <p>去掉3区大于4 5 6个数：{data.threeMore456.length}</p>
        <p>偶数6：{data.evenCount6.length}</p>
        <p>偶数5：{data.evenCount5.length}</p>
        <p>奇数6：{data.oddCount6.length}</p>
        <p>奇数5：{data.oddCount5.length}</p>
        <p>去掉对称数（比如01&02和32&33）：{data.drop1112And2223.length}</p>
        <p>红球2-红球1大于9：{data.red2BigRed1Nine.length}</p>
        <p>红6-红5 && 红4-红3 && 红2-红1 的结果同时=1 2 3 4 5 6 7 8 9 10 11时：{data.red654321jinge.length}</p>
        <p>红3-红1 = 10 && 红6-红4 = 10：{data.red3164dengyu10.length}</p>
        <p>去掉出现3同尾，或者4个1同尾的情况：{data.tongwei.length}</p>
        <p>去掉最近20期，红2-红1，红3-红2，红4-红3的相同间隔的记录{data.top20Red21Red32Red43.length}</p>
        <p>去掉红6-红1小于等于13：{data.red6red1.length}</p>
        <p>去掉最近20期的奇偶组合（比如：奇偶奇偶奇偶）：{data.dropTopOddAndEevn.length}</p>
      </div>

      <p>近10期</p>
      <table cellPadding={0}>
        <thead>
          <tr>
              <td>号码</td>
              <td>奇偶情况</td>
              <td>奇偶距上一次出现期数</td>
              <td>号码</td>
              <td>出现索引区间</td>
              <td>操作</td>
          </tr>
        </thead>
        <tbody>
          <HistoryList max={30} />
          {
            buyResult.map((item, index)=>{
              let itemStr = item.join("");
              let currentOddEven = getOddAndEven(item);
              let currentJiange = historyOddEvenList.indexOf(currentOddEven);
              let locationIndex = allRedBallStrList.indexOf(itemStr);
              
              return <tr key={index} className="chooseTr" data-currentoddeven={currentOddEven}>
                <td>{item.join(" ")}</td>
                <td>{currentOddEven}</td>
                <td>
                  {currentJiange + 1}
                  （间隔出现过{sortListObj[currentJiange + 1]}次）
                </td>
                <td>
                  <Red33List redBall={item}></Red33List>
                </td>
                <td>
                  <IndexLocation locationIndex={locationIndex}></IndexLocation>
                </td>
                <td>
                  {
                    index != 0
                    && <Button type="link" onClick={()=>{
                      let newList = buyResult.concat();
                      let c = newList.splice(index, 1);
                      newList = c.concat(newList);
                      setBuyResult(newList);
                    }}>移动到顶部</Button>
                  }
                </td>
              </tr>
            })
          }
          <tr>
            <td colSpan={6}>
              随机选择：随机产生总结果：{chooseList.length}个
              <p>
                {
                buyResult.length === 0
                ? "未过滤到结果"
                : ""
                }
              </p>
            </td>
          </tr>
        </tbody>
      </table>
      <div>
        筛选规则：
      </div>
      <Form
        ref={formRef} 
        name="basic"
        labelCol={{
          span: 8,
        }}
        wrapperCol={{
          span: 16,
        }}
        style={{
          maxWidth: 600,
        }}
        autoComplete="off"
        onFinish={(values)=>{
          setLoading(true);
          let sourceList = getChooseList({
            filterList: data.filterResult,
            values: values
          });

          //1.过滤掉杀号
          let removeNumber = values.filterNum;//下期杀号的数
          removeNumber  = removeNumber.trim()
          if(removeNumber.length){
            removeNumber = removeNumber.split(" ");
            sourceList = sourceList.filter((redBall)=>{
              let removeResult = needRemove(redBall, removeNumber);//true有重复 false没重复
              if(removeResult){//需要移除
                return false;
              }
              return true;
            })
          }

          //过滤掉奇偶组合间隔出现小于10次的
          //2.过滤掉奇偶组合间隔出现小于10次的
          sourceList = sourceList.filter((redBall)=>{
            let currentOddEven = getOddAndEven(redBall);
            let currentJiange = historyOddEvenList.indexOf(currentOddEven);
            let jiangeChuXianCount = sortListObj[currentJiange + 1];
            //如果奇偶数间隔出现次数小于10次，排除掉
            if(jiangeChuXianCount < 10){
              return false;
            }
            return true;
          })

          setChooseList(sourceList);//最终筛选的以红1开头的所有结果
          
          let result = sampleSize(sourceList, 10);//随机10条结果备选
          setBuyResult(result);
          setLoading(false);
        }}
      >
        <Form.Item
          label="开头数值"
          name="startNum"
        >
          <Input />
        </Form.Item>

        {/* <Form.Item
          label="选择数值"
          name="needNum"
        >
          <Input />
        </Form.Item>*/}

        <Form.Item
          name="filterNum"
          label="下期杀号"
        >
          <Input />
        </Form.Item>

        <Form.Item
          wrapperCol={{
            offset: 8,
            span: 16,
          }}
        >
          <Button type="primary" htmlType="submit">
            生成
          </Button>
        </Form.Item>
      </Form>

    </PageContainer>
  );
};

export default HomePage;